import type { User } from "@/typedefined/User";
import service, { http } from "./config"
import type { bgwSubject } from "@/typedefined/Subject";
import type { OjSubjectQueryCondition } from "@/typedefined/QueryCondition";
import type { SubjectAddDTO } from "@/typedefined/DTO";

/**
 * 用户相关接口
 */
export const userApi = {
    // 用户登录
    userLogin(code: string) {
        return http.post(`/auth/user/login?code=${code}`, null);
    },
    // 退出登录
    userLogout() {
        return http.get("/auth/user/exit", null);
    },
    // 获取用户信息
    getUserInfo() {
        return http.get(`/auth/user/info`, null);
    },
    // 上传头像
    uploadAvatar(file: File): Promise<string> {
        const formData = new FormData()
        formData.append('file', file)
        // 直接使用 service 发送请求
        return service({
            url: '/oss/file/upload',
            method: 'post',
            data: formData,
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })
    },
    // 更新用户头像
    updateAvatar(avatarUrl: string) {
        return http.patch(`/auth/user/info/avatar`, { avatar: avatarUrl })
    },
    // 更新用户信息
    updateUserInfo(user: User) {
        return http.patch(`/auth/user/update`, user);
    }
}

/**
 * 标签相关接口
 */
export const tagApi = {
    // 八股文标签获取
    getbgwTag() {
        return http.get('/subject/label/hot', null);
    },
    // oj标签获取
    getojTag() {
        return http.get("/oj/label/all", null);
    }
}

/**
 * 分类相关接口
 */
export const categoryApi = {
    // 八股文分类获取
    getbgwCategory(currentPage: number, pageSize: number) {
        const url = '/subject/category/one-two' + `?current_page=${currentPage}&page_size=${pageSize}`;
        return http.get(url, null);
    },
    // 八股文根据分类id获取二级分类列表
    getbgwCategoryById(categoryId: number) {
        return http.get(`/subject/category/byOneCategoryId?category_id=${categoryId}`, null);
    }
}

/**
 * 题目相关接口
 */
export const subjectApi = {
    // 八股文题目相关接口
    bgw: {
        // 添加题目
        addSubject(subject: bgwSubject) {
            return http.post("/subject/subject/add", subject);
        },
        // 按条件搜索题目
        searchSubject(params: any, currentPage: number, pageSize: number) {
            return http.post(`/subject/subject/condition?current_page=${currentPage}&page_size=${pageSize}`, params);
        },
        // 获取题目详情
        getSubjectDetail(subjectId: number) {
            return http.get(`/subject/subject/subjectId?subject_id=${subjectId}`, null);
        }
    },
    // oj题目相关接口
    oj: {
        // 按条件查询题目列表
        getSubjectList(queryCondition: OjSubjectQueryCondition) {
            return http.post("/oj/subject/query", queryCondition);
        },
        // 默认题目列表查询
        defaultQuerySubjectList(queryCondition: OjSubjectQueryCondition) {
            return http.post("/oj/subject/all", queryCondition);
        },
        // 随机一题
        randomSubject() {
            return http.get("/oj/subject/random", null);
        },
        // 代码提交
        codeSubmit(subjectId: number, userCode: string) {
            return http.post("/oj/submit/run", { subjectId, userCode });
        },
        // 获取代码执行结果
        getCodeRunResult(taskId: number) {
            return http.get(`/oj/submit/result?submitTaskId=${taskId}`, null);
        },
        // 新增题目
        addSubject(data:SubjectAddDTO){
            return http.post('/oj/subject/add',data);
        },
        // 题目点赞
        subjectThumb(subjectId:number){
            return http.post(`/oj/subject/thumb/enter?subject_id=${subjectId}`,null);
        },
        // 取消点赞
        cancelThumb(subjectId:number){
            return http.post(`/oj/subject/thumb/cancel?subject_id=${subjectId}`,null);
        },
        // 查询是否点赞
        isExistThumb(subjectId:number){
            return http.get(`/oj/subject/thumb/exist?subject_id=${subjectId}`,null);
        }
    }
}